
/**
 * Created by eku_mac on 9/6/17.
 */

import { ValuePair }    from './value-pair';
import { ProductNotes } from './product-note'
// import { ProductNote } from './product-note'
import { KeepaTime }    from './KeepaTime';

//产品详情页面
export class ProductDetail {

  dataReady:          boolean;
  asin:               string;
  domain_id:          string;
  title:              string;
  trackingSince:      number;
  last_update:        number;
  last_rating_update: number;
  last_price_change:  number;
  imagesCSV:          string;
  root_category:      number;
  categories:         number[];
  manufacturer:       string;
  brand:              string;

  publisher:          string;
  productGroup:       string;
  partNumber:         string;
  studio:             string;
  model:              string;

  publicationDate:number;
  color: string;
  size:  string;
  package_height: number;
  package_length: number;
  package_width:  number;
  package_weight: number;
  package_quantity: number;
  is_adult_product: boolean;
  is_eligible_for_tradein:              boolean;
  is_eligible_for_super_saver_shipping: boolean;
  product_type: number;
  from:         number;
  // ================================
  // rating datas
  ratingArrary:      any[] = [];
  ratingValueArrary: any[] = [];
  //==================================
  // reviews datas
  reviewsArray:      any[] = [];
  reviewsValueArray: any[] = [];
  // =================================
  // price datas
  priceArrary:      any[] = [];
  priceValueArrary: any[]=[];
  // =================================
  // rank datas
  rankArrary:      any[] = [];
  rankValueArrary: any[] = [];

  // new Date Arrary
  newDateArrary: any[] = [];

  //其他数据，存在track那边，渲染的时候用track去覆盖
  //release_date_by_user: number; //用户修正的上架日期. 数据库中也要添加这一行

  //下面这些数据可以单独API去获取
  //other data:
  notes: ProductNotes[];
  // notes: ProductNote[];
  tags: string[];

  ProductDetail() {
    this.dataReady = false;
  }

   public switchDays( days:string) {
      if(days == '0') {
        this.ratingValueArrary = this.ratingArrary.slice(0,this.ratingArrary.length-1);
        this.reviewsValueArray = this.reviewsArray.slice(0,this.reviewsArray.length-1);
        this.rankValueArrary   = this.rankArrary.slice(0,this.rankArrary.length-1);
        this.priceValueArrary  = this.priceArrary.slice(0,this.priceArrary.length-1);

      } else if (days == '7') {
        let last_rank   = this.rankArrary[this.rankArrary.length -1].x - 7 * 24 * 60 * 60 * 1000;
        let last_review = this.reviewsArray[this.reviewsArray.length -1].x - 7 * 24 * 60 * 60 * 1000;
        let last_rating = this.ratingArrary[this.ratingArrary.length -1].x - 7 * 24 * 60 * 60 * 1000;
        let last_price  = this.priceArrary[this.priceArrary.length -1].x - 7 * 24 * 60 * 60 * 1000;

        let temp: any[] = [];

        for (let i = (this.rankArrary.length -1); i > 0; i--) {
          if ( this.rankArrary[i].x > last_rank) {
              temp.push(this.rankArrary[i]);
          }
        }
        this.rankValueArrary = temp;

        temp = [];
        for (let i = (this.reviewsArray.length -1); i > 0; i--) {
          if (this.reviewsArray[i].x > last_review) {
            temp.push(this.reviewsArray[i]);
          }
        }
        this.reviewsValueArray = temp;

        temp = [];
        for (let i = (this.ratingArrary.length - 1); i > 0; i--) {
          if (this.ratingArrary[i].x > last_rating) {
            temp.push(this.ratingArrary[i]);
          }
        }

        this.ratingValueArrary = temp;

        temp = [];
        for (let i = (this.priceArrary.length -1); i > 0; i--) {
          if (this.priceArrary[i].x > last_price) {
            temp.push(this.priceArrary[i]);
          }
        }
        this.priceValueArrary = temp;

     } else if (days == '30') {
        let last_rank   = this.rankArrary[this.rankArrary.length -1].x - 30 * 24 * 60 * 60 * 1000;
        let last_review = this.reviewsArray[this.reviewsArray.length -1].x - 30 * 24 * 60 * 60 * 1000;
        let last_rating = this.ratingArrary[this.ratingArrary.length -1].x - 30 * 24 * 60 * 60 * 1000;
        let last_price  = this.priceArrary[this.priceArrary.length - 1].x - 30 * 24 * 60 * 60 * 1000;

        let temp: any[] = [];

        for (let i = (this.rankArrary.length - 1); i > 0; i--) {
          if (this.rankArrary[i].x > last_rank) {
            temp.push(this.rankArrary[i]);
          }
        }

        this.rankValueArrary = temp;

        temp = [];
        for (let i = (this.reviewsArray.length - 1); i > 0; i--) {
          if (this.reviewsArray[i].x > last_review) {
            temp.push(this.reviewsArray[i]);
          }
        }
        this.reviewsValueArray = temp;

        temp = [];
        for (let i = (this.ratingArrary.length - 1); i > 0; i--) {
          if (this.ratingArrary[i].x > last_rating) {
            temp.push(this.ratingArrary[i]);
          }
        }

        this.ratingValueArrary = temp;

        temp = [];
        for (let i = (this.priceArrary.length - 1); i > 0; i--) {
          if (this.priceArrary[i].x > last_price) {
            temp.push(this.priceArrary[i]);
          }
        }
        this.priceValueArrary = temp;
      } else if (days == '90') {
        let last_rank   = this.rankArrary[this.rankArrary.length -1].x - 90 * 24 * 60 * 60 * 1000;
        let last_review = this.reviewsArray[this.reviewsArray.length -1].x - 90 * 24 * 60 * 60 * 1000;
        let last_rating = this.ratingArrary[this.ratingArrary.length -1].x - 90 * 24 * 60 * 60 * 1000;
        let last_price  = this.priceArrary[this.priceArrary.length -1].x - 90 * 24 * 60 * 60 * 1000;

        let temp: any[] = [];

        for (let i = (this.rankArrary.length - 1); i > 0; i--) {
          if (this.rankArrary[i].x > last_rank) {
            temp.push(this.rankArrary[i]);
          }
        }

        this.rankValueArrary = temp;
        console.log("90", this.rankValueArrary);

        temp = [];
        for (let i = (this.reviewsArray.length - 1); i > 0; i--) {
          if (this.reviewsArray[i].x > last_review) {
            temp.push(this.reviewsArray[i]);
          }
        }

        this.reviewsValueArray = temp;
        console.log("90", this.reviewsValueArray);

        temp = [];
        for (let i = (this.ratingArrary.length - 1); i > 0; i--) {
          if (this.ratingArrary[i].x > last_rating) {
            temp.push(this.ratingArrary[i]);
          }
        }

        this.ratingValueArrary = temp;
        console.log("90", this.ratingValueArrary);

        temp = [];

        for (let i = (this.priceArrary.length - 1); i > 0; i--) {
          if (this.priceArrary[i].x > last_price) {
            temp.push(this.priceArrary[i]);
          }
        }
        this.priceValueArrary = temp;
      }
   }

   public ParseFromJason( product: any) {
     this.asin         = product.asin;
     this.title        = product.title;
     this.categories   = product.categories;
     this.manufacturer = product.manufacturer;
     this.brand        = product.brand;
     this.publisher    = product.publisher;
     this.size         = product.size;

    //  this.rankArrary = product.rank;
    //  this.ratingArrary = product.rating;
    //  this.reviewsArrary = product.reviews;
    //  this.priceArrary = product.price;

    let theDate:Date ;
    let theValue:any;

    for (let i = 0; i < product.cvs_ranks.length; i++) {
      if (i % 2 == 0) {
        theDate = KeepaTime.KeepaTimeToTime(product.cvs_ranks[i]);
      } else if (i % 2 == 1 ) {
        theValue = product.cvs_ranks[i];
        this.rankArrary.push( {x:theDate.getTime(), y:theValue});
      }
    }

    for (let i = 0; i < product.cvs_ratings.length; i++) {
      if (i % 2 == 0) {
        theDate = KeepaTime.KeepaTimeToTime(product.cvs_ratings[i]);
        //this.ratingDateArrary.push(date);
      } else if (i % 2 != 0) {
        theValue = product.cvs_ratings[i]/10.0;
        this.ratingArrary.push( {x:theDate.getTime(), y:theValue});
      }
    }

    for (let i = 0; i < product.cvs_prices.length; i++) {
      if (i % 2 == 0) {
        theDate = KeepaTime.KeepaTimeToTime(product.cvs_prices[i]);
      } else if (i % 2 !=0) {
        theValue = product.cvs_prices[i] / 100.00;
        this.priceArrary.push( {x:theDate.getTime(), y:theValue});
      }
    }

    for (let i = 0; i < product.cvs_reviews.length; i++) {
      if (i % 2 == 0) {
        theDate = KeepaTime.KeepaTimeToTime(product.cvs_reviews[i]);
      } else if (i % 2 != 0) {
        theValue = product.cvs_reviews[i];
        this.reviewsArray.push({x:theDate.getTime(), y:theValue});
      }
    }

    this.switchDays('0');
    this.dataReady = true;
  }

}
